package cc.mrbird.unit.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.system.domain.User;
import cc.mrbird.unit.domain.AuxiliaryInfor;
import cc.mrbird.unit.domain.UnitType;
import cc.mrbird.unit.service.AuxiliaryInforService;
import cc.mrbird.unit.utils.FileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Api(description = "辅助信息接口")
@RestController
public class AuxiliaryInforController  extends BaseController{
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FileUtils fileUtils;
    @Autowired
    private AuxiliaryInforService auxiliaryInforService;
    @Log("获取辅助信息")
    @RequestMapping(value = "auxiliary",method = RequestMethod.POST)
    /* @RequiresPermissions("auxiliary:list")*/
    public String index() {
        return "unit/auxiliary/auxiliary";
    }
    @Log("获取辅助信息")
    @ApiOperation(value = "获取辅助信息" ,  notes="获取辅助信息")
    @RequestMapping(value = "auxiliary/list",method = RequestMethod.POST)
    /*@RequiresPermissions("auxiliary:list")*/
    @ResponseBody
    public Map<String, Object> auxiliaryList(QueryRequest request, AuxiliaryInfor auxiliaryInfor) {
        return super.selectByPageNumSize(request, () -> this.auxiliaryInforService.findAllList(auxiliaryInfor, request));
    }

    @RequestMapping(value = "auxiliary/add", method = RequestMethod.POST)
    public ResponseBo add(@RequestParam("fileOne") MultipartFile fileOne,@RequestParam("fileTwo") MultipartFile fileTwo ,AuxiliaryInfor auxiliaryInfor){
        User currentUser = super.getCurrentUser();
        if (fileUtils.upload(fileOne,fileTwo,auxiliaryInfor,currentUser)){
            // 上传成功，给出页面提示
            return ResponseBo.ok();
        }else {
            return ResponseBo.error("上传失败，请重新上传！");
        }
    }

    @RequestMapping(value = "auxiliary/update", method = RequestMethod.POST)
    public ResponseBo update(@RequestParam("fileOne") MultipartFile fileOne,@RequestParam("fileTwo") MultipartFile fileTwo, AuxiliaryInfor auxiliaryInfor){
        if (fileUtils.update(fileOne,fileTwo,auxiliaryInfor)){
            // 上传成功，给出页面提示
            return ResponseBo.ok("修改辅助信息成功");
        }else {
            return ResponseBo.error("修改辅助信息失败，请联系网站管理员！");
        }
    }
    /*@ApiOperation(value = "修改辅助信息信息")
    @Log("修改辅助信息信息")
    *//* @RequiresPermissions("auxiliary:update")*//*
    @RequestMapping(value = "auxiliary/update" ,method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo update(AuxiliaryInfor auxiliaryInfor) {
        try {
            this.auxiliaryInforService.update(auxiliaryInfor);
            return ResponseBo.ok("修改辅助信息成功！");
        } catch (Exception e) {
            log.error("修改辅助信息失败", e);
            return ResponseBo.error("修改辅助信息失败，请联系网站管理员！");
        }
    }*/
    @Log("删除辅助信息")
    /*@RequiresPermissions("auxiliary:delete")*/
    @RequestMapping(value = "auxiliary/delete",method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo delete(String ids) {
        try {
            this.auxiliaryInforService.delete(ids);
            return ResponseBo.ok("删除辅助信息成功！");
        } catch (Exception e) {
            log.error("删除辅助信息失败", e);
            return ResponseBo.error("删除辅助信息失败，请联系网站管理员！");
        }
    }

}
